package org.yu.register;

import com.mojang.datafixers.util.Pair;
import net.minecraft.core.NonNullList;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.data.recipes.ShapedRecipeBuilder;
import net.minecraft.data.recipes.ShapelessRecipeBuilder;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.SimpleRecipeSerializer;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.common.Tags;
import org.yu.Yu;
import org.yu.api.register.RecipeHolder;
import org.yu.api.register.data.VanillaRecipeProvider;
import org.yu.register.recipe.BagSealedRecipe;
import org.yu.register.recipe.ImpactRecipe;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class RecipeRegister {
    public static final RecipeHolder<ImpactRecipe> impactRecipe= Yu.LAZYREGISTRY.recipe("impact",new ImpactRecipe.Serializer());
     public static final RecipeSerializer<BagSealedRecipe> bagSealed =Yu.LAZYREGISTRY.customRecipe("bag_sealed",new SimpleRecipeSerializer<>(BagSealedRecipe::new));
    public static void init(){
        var iron=Ingredient.of(Items.IRON_INGOT);
        Yu.LAZYREGISTRY.executeWhenDataStart(()->{
            ImpactRecipe impactRecipe=new ImpactRecipe(NonNullList.of(
                    Ingredient.EMPTY,
                    iron,iron,iron,
                    iron,Ingredient.of(ItemRegister.fireSeed),iron,
                    iron,iron,iron
            ),"Awaken");
            Yu.LAZYREGISTRY.dumpRecipe("awaken",impactRecipe);
            Yu.LAZYREGISTRY.dumpVanillaRecipe((finishedRecipeConsumer, vanillaRecipeProvider) -> {
                ShapedRecipeBuilder.shaped(ItemRegister.judgementOfShamash)
                        .unlockedBy("hasFireSeed", vanillaRecipeProvider.hasItem(ItemRegister.fireSeed))
                        .pattern("  A")
                        .pattern(" A ")
                        .pattern("B  ")
                        .define('A', ItemRegister.fireSeed)
                        .define('B', Items.BLAZE_ROD)
                        .save(finishedRecipeConsumer);
                ShapedRecipeBuilder.shaped(BlockRegister.lacquerDoor,3)
                        .unlockedBy("hasPlank", vanillaRecipeProvider.hasItem(ItemTags.PLANKS))
                        .pattern("AA")
                        .pattern("AA")
                        .pattern("AA")
                        .define('A', BlockRegister.lacquerPlank)
                        .save(finishedRecipeConsumer);
                ShapedRecipeBuilder.shaped(ItemRegister.silkBag)
                        .unlockedBy("hasWool", vanillaRecipeProvider.hasItem(ItemTags.WOOL))
                        .pattern("A A")
                        .pattern("ABA")
                        .pattern("AAA")
                        .define('A', ItemTags.WOOL)
                        .define('B',Tags.Items.LEATHER)
                        .save(finishedRecipeConsumer);
                ShapedRecipeBuilder.shaped(MachineRegistry.llandudno)
                        .unlockedBy("hasCopper", vanillaRecipeProvider.hasItem(Tags.Items.INGOTS_COPPER))
                        .pattern("A A")
                        .pattern("ABA")
                        .pattern("AAA")
                        .define('A',Tags.Items.INGOTS_COPPER)
                        .define('B',Items.BUCKET)
                        .save(finishedRecipeConsumer);
                ShapedRecipeBuilder.shaped(MachineRegistry.stove)
                        .unlockedBy("hasFurnace", vanillaRecipeProvider.hasItem(Blocks.FURNACE))
                        .pattern("A A")
                        .pattern("ABA")
                        .pattern("AAA")
                        .define('A',Tags.Items.COBBLESTONE)
                        .define('B',Blocks.FURNACE)
                        .save(finishedRecipeConsumer);
                ShapedRecipeBuilder.shaped(ItemRegister.refractoryTank)
                        .unlockedBy("hasRefractoryClay", vanillaRecipeProvider.hasItem(ItemRegister.refractoryClay))
                        .pattern("A A")
                        .pattern("A A")
                        .pattern("AAA")
                        .define('A', ItemRegister.refractoryClay)
                        .save(finishedRecipeConsumer);
                ShapedRecipeBuilder.shaped(BlockRegister.lacquerTrapdoor,4)
                        .unlockedBy("hasPlank", vanillaRecipeProvider.hasItem(ItemTags.PLANKS))
                        .pattern("AAA")
                        .pattern("AAA")
                        .define('A', BlockRegister.lacquerPlank)
                        .save(finishedRecipeConsumer);
                ShapelessRecipeBuilder.shapeless(BlockRegister.lacquerPlank,4)
                        .unlockedBy("hasLog", vanillaRecipeProvider.hasItem(ItemTags.LOGS))
                        .requires(BlockRegister.lacquerLog)
                        .save(finishedRecipeConsumer);
                ShapelessRecipeBuilder.shapeless(BlockRegister.lacquerButton)
                        .unlockedBy("hasPlank", vanillaRecipeProvider.hasItem(ItemTags.PLANKS))
                        .requires(BlockRegister.lacquerPlank)
                        .save(finishedRecipeConsumer);
                ShapelessRecipeBuilder.shapeless(BlockRegister.lacquerLog,3)
                        .unlockedBy("hasLog", vanillaRecipeProvider.hasItem(ItemTags.LOGS))
                        .requires(ItemTags.LOGS)
                        .requires(ItemTags.LOGS)
                        .requires(ItemTags.LOGS)
                        .requires(Tags.Items.DYES_RED)
                        .save(finishedRecipeConsumer);
                ShapelessRecipeBuilder.shapeless(BlockRegister.jadeBlock)
                        .unlockedBy("hasJade", vanillaRecipeProvider.hasItem(ItemRegister.jade))
                        .requires(ItemRegister.jade)
                        .requires(ItemRegister.jade)
                        .requires(ItemRegister.jade)
                        .requires(ItemRegister.jade)
                        .save(finishedRecipeConsumer);
                ShapelessRecipeBuilder.shapeless(ItemRegister.refractoryClay)
                        .unlockedBy("hasClay", vanillaRecipeProvider.hasItem(Items.CLAY_BALL))
                        .requires(Items.BLAZE_POWDER)
                        .requires(Items.CLAY_BALL)
                        .save(finishedRecipeConsumer);
                ShapelessRecipeBuilder.shapeless(ItemRegister.jade,4)
                        .unlockedBy("hasJadeBlock", vanillaRecipeProvider.hasItem(BlockRegister.jadeBlock))
                        .requires(BlockRegister.jadeBlock)
                        .save(finishedRecipeConsumer);
                ShapelessRecipeBuilder.shapeless(ItemRegister.fireSeed)
                        .unlockedBy("hasCoal", vanillaRecipeProvider.hasItem(ItemTags.COALS))
                        .requires(ItemTags.LOGS)
                        .requires(ItemTags.COALS)
                        .requires(Items.LAVA_BUCKET)
                        .requires(Items.TORCH)
                        .save(finishedRecipeConsumer);
            });
        });

    }

    private static <A,B> Pair<A,B> pair(A a, B b){
        return new Pair<>(a,b);
    }
}
